<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户uid" prop="userUid">
      <el-input v-model="dataForm.userUid" placeholder="用户uid"></el-input>
    </el-form-item>
    <el-form-item label="商家用户uid" prop="merchanuUserUid">
      <el-input v-model="dataForm.merchanuUserUid" placeholder="商家用户uid"></el-input>
    </el-form-item>
    <el-form-item label="订单状态：0：待支付，1：服务中，2：已完成，3：已取消" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态：0：待支付，1：服务中，2：已完成，3：已取消"></el-input>
    </el-form-item>
    <el-form-item label="支付方式: 1: 积分支付, 2: 支付宝、3: 微信" prop="payMethod">
      <el-input v-model="dataForm.payMethod" placeholder="支付方式: 1: 积分支付, 2: 支付宝、3: 微信"></el-input>
    </el-form-item>
    <el-form-item label="资源类型" prop="resourceType">
      <el-input v-model="dataForm.resourceType" placeholder="资源类型"></el-input>
    </el-form-item>
    <el-form-item label="资源uid" prop="resourceUid">
      <el-input v-model="dataForm.resourceUid" placeholder="资源uid"></el-input>
    </el-form-item>
    <el-form-item label="订单金额" prop="price">
      <el-input v-model="dataForm.price" placeholder="订单金额"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="productNum">
      <el-input v-model="dataForm.productNum" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="资源描述" prop="summary">
      <el-input v-model="dataForm.summary" placeholder="资源描述"></el-input>
    </el-form-item>
    <el-form-item label="商品快照" prop="snapshot">
      <el-input v-model="dataForm.snapshot" placeholder="商品快照"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          userUid: '',
          merchanuUserUid: '',
          orderStatus: '',
          payMethod: '',
          resourceType: '',
          resourceUid: '',
          price: '',
          productPrice: '',
          productNum: '',
          title: '',
          summary: '',
          snapshot: '',
          status: '',
          createTime: '',
          updateTime: '',
          isDelete: ''
        },
        dataRule: {
          userUid: [
            { required: true, message: '用户uid不能为空', trigger: 'blur' }
          ],
          merchanuUserUid: [
            { required: true, message: '商家用户uid不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态：0：待支付，1：服务中，2：已完成，3：已取消不能为空', trigger: 'blur' }
          ],
          payMethod: [
            { required: true, message: '支付方式: 1: 积分支付, 2: 支付宝、3: 微信不能为空', trigger: 'blur' }
          ],
          resourceType: [
            { required: true, message: '资源类型不能为空', trigger: 'blur' }
          ],
          resourceUid: [
            { required: true, message: '资源uid不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '订单金额不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          productNum: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '资源描述不能为空', trigger: 'blur' }
          ],
          snapshot: [
            { required: true, message: '商品快照不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/payorder/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userUid = data.payOrder.userUid
                this.dataForm.merchanuUserUid = data.payOrder.merchanuUserUid
                this.dataForm.orderStatus = data.payOrder.orderStatus
                this.dataForm.payMethod = data.payOrder.payMethod
                this.dataForm.resourceType = data.payOrder.resourceType
                this.dataForm.resourceUid = data.payOrder.resourceUid
                this.dataForm.price = data.payOrder.price
                this.dataForm.productPrice = data.payOrder.productPrice
                this.dataForm.productNum = data.payOrder.productNum
                this.dataForm.title = data.payOrder.title
                this.dataForm.summary = data.payOrder.summary
                this.dataForm.snapshot = data.payOrder.snapshot
                this.dataForm.status = data.payOrder.status
                this.dataForm.createTime = data.payOrder.createTime
                this.dataForm.updateTime = data.payOrder.updateTime
                this.dataForm.isDelete = data.payOrder.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/payorder/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'userUid': this.dataForm.userUid,
                'merchanuUserUid': this.dataForm.merchanuUserUid,
                'orderStatus': this.dataForm.orderStatus,
                'payMethod': this.dataForm.payMethod,
                'resourceType': this.dataForm.resourceType,
                'resourceUid': this.dataForm.resourceUid,
                'price': this.dataForm.price,
                'productPrice': this.dataForm.productPrice,
                'productNum': this.dataForm.productNum,
                'title': this.dataForm.title,
                'summary': this.dataForm.summary,
                'snapshot': this.dataForm.snapshot,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
